/**
 * 
 */
package com.khotyn.heresy.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.khotyn.heresy.bean.HeresyErrorMessage;
import com.khotyn.heresy.bean.PagedBean;
import com.khotyn.heresy.bean.PictureDetail;
import com.khotyn.heresy.dao.PictureDAO;
import com.khotyn.heresy.dao.UserDAO;
import com.khotyn.heresy.exception.IllegalUrlParamException;

/**
 * 审核图片的控制器
 * 
 * @author khotyn
 * 
 */
@Controller
public class checkPictureController {

	@Autowired
	private PictureDAO pictureDAO;
	@Autowired
	private UserDAO userDAO;
	private static int pageSize = 1; // 每页显示的数据数量
	private static int punishScore = 20; // 惩罚分数
	private static String illegalPicNotice = "illegalPicNotice.jpg";

	@RequestMapping(value = "/checkPicture.html", method = RequestMethod.GET)
	public ModelAndView setupCheckPictureForm(ModelMap model, HttpSession session, @RequestParam(value = "page", required = false) String pageStr) {
		Integer adminID = (Integer) session.getAttribute("adminID");

		if (pageStr == null) {
			pageStr = "1";
		}

		try {
			validate(pageStr, adminID);
		} catch (IllegalUrlParamException e) {
			return e.getErrorModel();
		}

		int page = NumberUtils.toInt(pageStr);
		PagedBean pagedBean = new PagedBean();
		pagedBean.setPageNum(page);
		pagedBean.setPageSize(pageSize);

		int minRow = (page - 1) * pageSize + 1;
		int maxRow = page * pageSize;
		pagedBean.setPagedItem(pictureDAO.selectReportedPicture(minRow, maxRow));
		pagedBean.setItemNum(pictureDAO.selectReportedPictureCount());

		int maxPage = (int) java.lang.Math.ceil((double) pagedBean.getItemNum() / (double) pageSize);
		pagedBean.setMaxPage(maxPage);

		PagedBean pictureList = pagedBean;
		return new ModelAndView("checkPicture", "pictureList", pictureList);
	}

	@RequestMapping(value = "/checkPicture.html", method = RequestMethod.POST)
	public ModelAndView checkPicture(@RequestParam(value = "pictureID", required = true) String pictureID,
			@RequestParam(value = "check", required = true) String check) {

		if (check.equalsIgnoreCase("pass")) {
			pictureDAO.updatePictureCheckFlag(pictureID, "pass");
		} else if (check.equalsIgnoreCase("notpass")) {
			pictureDAO.updatePictureCheckFlag(pictureID, "notpass");
			// 修改图片的可删除标记位
			PictureDetail picDetail = new PictureDetail();
			picDetail.setPictureID(pictureID);
			picDetail.setFlagDelete("Yes");
			pictureDAO.updatePictureDelFlag(picDetail);

			// 告知收藏该图片的用户图片已经被删除，将图片的路径修改成通知图片的地址
			pictureDAO.updatePicLocation(pictureID, illegalPicNotice);

			// 惩罚上传该图片的用户，更新用户分数
			Integer userID = pictureDAO.selectPicOwnerId(pictureID);
			Integer userPoint = userDAO.selectUserPoint(userID);
			userPoint -= punishScore;

			if (userPoint < 0)
				userPoint = 0;

			userDAO.updateScore(userID, userPoint);
		} else {

		}

		return new ModelAndView("checkPicture");
	}

	private void validate(String pageStr, Integer adminID) throws IllegalUrlParamException {
		HeresyErrorMessage errorMessage = null;

		if (adminID == null) {
			errorMessage = new HeresyErrorMessage("您尚未登录，请先登录", "请登录", "adminLogin.html");
		} else if (NumberUtils.isDigits(pageStr)) {
			errorMessage = new HeresyErrorMessage("非法的参数", "操作失败", "/Dragonfly/");
		}

		if (errorMessage != null) {
			throw new IllegalUrlParamException(errorMessage);
		}
	}
}
